<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.http.HttpSession" %>
<%@ page import="java.sql.*" %>
<%@ page session="true" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>登录页面 - 网吧管理系统</title>
    <link rel="icon" href="favicon.ico" type="image/ico">
    <meta name="keywords" content="网吧管理系统, 登录, 后台管理系统">
    <meta name="description" content="网吧管理系统后台管理">
    <meta name="author" content="网吧管理团队">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/materialdesignicons.min.css" rel="stylesheet">
    <link href="css/style.min.css" rel="stylesheet">
    <style>
        .lyear-wrapper {
            position: relative;
        }
        .lyear-login {
            display: flex !important;
            min-height: 100vh;
            align-items: center !important;
            justify-content: center !important;
        }
        .lyear-login:after {
            content: '';
            min-height: inherit;
            font-size: 0;
        }
        .login-center {
            background: #fff;
            min-width: 29.25rem;
            padding: 2.14286em 3.57143em;
            border-radius: 3px;
            margin: 2.85714em;
        }
        .login-header {
            margin-bottom: 1.5rem !important;
        }
        .login-center .has-feedback.feedback-left .form-control {
            padding-left: 38px;
            padding-right: 12px;
        }
        .login-center .has-feedback.feedback-left .form-control-feedback {
            left: 0;
            right: auto;
            width: 38px;
            height: 38px;
            line-height: 38px;
            z-index: 4;
            color: #dcdcdc;
        }
        .login-center .has-feedback.feedback-left.row .form-control-feedback {
            left: 15px;
        }
    </style>
</head>
<body>
<div class="row lyear-wrapper" style="background-image: url(images/login-bg.jpg); background-size: cover;">
    <div class="lyear-login">
        <div class="login-center">
            <div class="login-header text-center">
                <a href="login.jsp"><img alt="网吧管理系统" src="images/logo-sidebar.png"></a>
            </div>
            <form action="login.jsp" method="post">
                <!-- 用户名 -->
                <div class="form-group has-feedback feedback-left">
                    <input type="text" placeholder="请输入您的用户名" class="form-control" name="username" id="username" required />
                    <span class="mdi mdi-account form-control-feedback" aria-hidden="true"></span>
                </div>

                <!-- 密码 -->
                <div class="form-group has-feedback feedback-left">
                    <input type="password" placeholder="请输入密码" class="form-control" id="password" name="password" required />
                    <span class="mdi mdi-lock form-control-feedback" aria-hidden="true"></span>
                </div>

                <!-- 验证码 -->
                <div class="form-group has-feedback feedback-left row">
                    <div class="col-xs-7">
                        <input type="text" name="captcha" class="form-control" placeholder="验证码" required>
                        <span class="mdi mdi-check-all form-control-feedback" aria-hidden="true"></span>
                    </div>
                    <div class="col-xs-5">
                        <img src="images/captcha.png" class="pull-right" id="captcha" style="cursor: pointer;" onclick="this.src=this.src+'?d='+Math.random();" title="点击刷新" alt="captcha">
                    </div>
                </div>

                <!-- 自动登录 -->
                <div class="form-group">
                    <label class="lyear-checkbox checkbox-primary m-t-10">
                        <input type="checkbox" name="autoLogin"><span>5天内自动登录</span>
                    </label>
                </div>

                <!-- 登录按钮 -->
                <div class="form-group">
                    <button class="btn btn-block btn-primary" type="submit">立即登录</button>
                </div>
            </form>

            <hr>

            <!-- 页面底部 -->
            <footer class="col-sm-12 text-center">
                <p class="m-b-0">Copyright © 2024 <a href="#">网吧管理系统</a>. All rights reserved</p>
            </footer>
        </div>
    </div>
</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript">
    // 可以添加一些前端验证或JavaScript功能
</script>

<%-- 处理用户登录 --%>
<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    if (username != null && password != null) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            // 数据库连接
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/netbar", "root", "rjxy123456");

            // 查询管理员表
            String sql = "SELECT password FROM admin WHERE username = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();

            if (rs.next()) {
                String storedPassword = rs.getString("password");

                // 检查密码是否匹配
                if (storedPassword.equals(password)) {
                    HttpSession _session = request.getSession();
                    _session.setAttribute("username", username);

                    response.sendRedirect("StatisticsServlet"); // 登录成功跳转
                } else {
                    out.println("<script>alert('密码错误');</script>");
                }
            } else {
                out.println("<script>alert('用户不存在');</script>");
            }
        } catch (Exception e) {
            e.printStackTrace();
            out.println("<script>alert('系统错误，请稍后重试');</script>");
        } finally {
            if (rs != null) rs.close();
            if (ps != null) ps.close();
            if (conn != null) conn.close();
        }
    }
%>

</body>
</html>
